package cn.tedu._05weibo.controller;


import cn.tedu._05weibo.base.response.JsonResult;
import cn.tedu._05weibo.base.response.StatusCode;
import cn.tedu._05weibo.mapper.CommentMapper;
import cn.tedu._05weibo.pojo.dto.CommentAddParam;
import cn.tedu._05weibo.pojo.entity.Comment;
import cn.tedu._05weibo.pojo.vo.CommentVO;
import cn.tedu._05weibo.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/comment/")
@Api(tags = "03.评论模块")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("insert")
    @ApiOperation(value = "发表评论")
    @ApiOperationSupport(order = 10)
    public JsonResult insert(@RequestBody CommentAddParam commentAddParam, @ApiIgnore HttpSession session){
        log.debug("commentAddParam: "+commentAddParam);
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentAddParam,comment);
        comment.setCreateTime(new Date());
        UserVO userVO = (UserVO) session.getAttribute("user");
        comment.setUserId(userVO.getId());
        log.debug("comment: "+comment);
        int count = commentMapper.insertComment(comment);
        return count>0?JsonResult.ok():new JsonResult(StatusCode.OPERATION_FAILED);
    }

    @GetMapping("selectByWeiboId")
    @ApiOperation(value = "获取评论列表")
    @ApiOperationSupport(order = 20)
    @ApiImplicitParam(name = "weiboId",value = "微博编号",required = true)
    public JsonResult selectByWeiboId(Long weiboId){
        log.debug("微博编号: "+weiboId);
        List<CommentVO> commentVOList = commentMapper.selectCommentByWeiboId(weiboId);
        return commentVOList!=null?JsonResult.ok(commentVOList):new JsonResult(StatusCode.OPERATION_FAILED);
    }

}
